local skel = fk.CreateSkill {
  name = "emo__jiejie",
}

Fk:loadTranslationTable{
  ["emo__jiejie"] = "竭诘",
  [":emo__jiejie"] = "出牌阶段开始时，你可以令你本阶段：对使用牌无次数、目标限制；仅能使用点数1~13的手牌，当你使用点数在限制点数开区间内的牌时，令限制点数上下限中最接近的一个修改至此点数，然后摸两张牌；本阶段结束时，弃置所有限制点数之外的手牌。",

  ["@emo__jiejie-phase"] = "竭诘",
  ["#emo__jiejie-choice"] = "竭诘：修改你本阶段可以使用牌的点数范围",
  ["#emo__jiejie-use"] = "竭诘：将一张牌对任意目标使用",
}

---@param player Player
---@return integer[]?
local function getLimitNum(player)
  local mark = player:getMark("@emo__jiejie-phase")
  if mark == 0 then return nil end
  local list = string.split(mark, "~")
  return table.map(list, function (e) return tonumber(e) end)
end

skel:addEffect("active", {
  anim_type = "control",
  prompt = "#emo__jiejie-use",
  can_use = function(self, player)
    return player:getMark("@emo__jiejie-phase") ~= 0
  end,
  card_num = 1,
  card_filter = function (self, player, to_select, selected)
    if #selected ~= 0 or not  table.contains(player:getCardIds("h"), to_select) then return false end
    local card = Fk:getCardById(to_select)
    return not player:prohibitUse(card) and not card.is_passive
  end,
  target_filter = function (self, player, to, selected, cards)
    if #cards == 0 then return false end
    local card = Fk:getCardById(cards[1])
    if (card.type == Card.TypeEquip or card.sub_type == Card.SubtypeDelayedTrick) and #selected ~= 0 then return false end
    if card.skill:getMinTargetNum(player) > 1 and #selected >= card.skill:getMinTargetNum(player) then
      return false
    end
    return not player:isProhibited(to, card)
  end,
  min_target_num = 1,
  on_use = function(self, room, effect)
    local player = effect.from
    room:useCard{
      from = player,
      tos = effect.tos,
      card = Fk:getCardById(effect.cards[1]),
      extraUse = true,
    }
  end
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Play
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@emo__jiejie-phase", "1~13")
  end,
})

skel:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    local limit = getLimitNum(player)
    if limit then
      local cards = Card:getIdList(card)
      if #cards == 0 or table.find(cards, function (id) return not table.contains(player:getCardIds("h"), id) end) then
        return true -- 非手牌直接禁止
      end
      local min, max = table.unpack(limit)
      return card.number < min or card.number > max
    end
  end,
})

skel:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return card and player:getMark("@emo__jiejie-phase") ~= 0
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    return card and player:getMark("@emo__jiejie-phase") ~= 0
  end,
})

skel:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      local limit = getLimitNum(player)
      if limit then
        local min, max = table.unpack(limit)
        return data.card.number > min and data.card.number < max
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local num = data.card.number
    local limit = getLimitNum(player)
    if not limit then return end
    local min, max = table.unpack(limit)
    local ret
    local max_dist, min_dist = math.abs(max - num), math.abs(min - num)
    if max_dist < min_dist then
      ret = min .. "~" .. num
    elseif min_dist < max_dist then
      ret = num .. "~" .. max
    else
      local choices = { min .. "~" .. num, num .. "~" .. max }
      ret = room:askToChoice(player, {choices = choices, skill_name = skel.name, prompt = "#emo__jiejie-choice"})
    end
    room:setPlayerMark(player, "@emo__jiejie-phase", ret)
    player:drawCards(2, skel.name)
  end,
})

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "negative",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return player:getMark("@emo__jiejie-phase") ~= 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local limit = getLimitNum(player)
    if not limit then return end
    local min, max = table.unpack(limit)
    local cards = table.filter(player:getCardIds("h"), function (id)
      local card = Fk:getCardById(id)
      return card.number < min or card.number > max
    end)
    if #cards > 0 then
      room:throwCard(cards, skel.name, player, player)
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__jiejie-phase", 0)
end)

return skel
